
package game.view.scene {
	import common.IRender;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.utils.getTimer;
	
	import game.manager.StageManager;
	import game.model.SceneModel;
	import game.model.vo.SceneVO;
	
	
	/**
	 * MapLayer
	 *
	 * @author chris
	 *
	 * Apr 22, 2013
	 */
	public class MapLayer extends Sprite implements IRender{
		private var buffer:BitmapData;
		private var bitmap:Bitmap;
		
		private var lastScrollX:int=0;
		private var lastScrollY:int=0;
		private var lastTime:int = 0;
		
		public var TILE_WIDTH:int = 300;
		public var TILE_HEIGHT:int = 300;
		public var SCREEN_WIDTH:int = 900;
		public var SCREEN_HEIGHT:int = 600;
		public var SCENE_WIDTH:int = 2560;
		public var SCENE_HEIGHT:int = 2880;
		public var GRID_WIDTH:int = 20;
		public var GRID_HEIGHT:int = 10;
		
		public function MapLayer()
		{
			super();
		}
		
		public function init(vo:SceneVO):void {
			SCREEN_WIDTH = StageManager.inst.screenWith;
			SCREEN_HEIGHT = StageManager.inst.screenHeight;
			SCENE_WIDTH = vo.width;
			SCENE_HEIGHT = vo.height;
			TILE_WIDTH = vo.tileWidth;
			TILE_HEIGHT = vo.tileHeight;
			GRID_WIDTH = vo.gridWith;
			GRID_HEIGHT = vo.gridHeight;
			
			buffer = new BitmapData(SCREEN_WIDTH + TILE_WIDTH*2,SCREEN_HEIGHT + TILE_HEIGHT*2,true,0x00000000);
			bitmap = new Bitmap(buffer);
			this.addChild(bitmap);
			bitmap.x = - TILE_WIDTH;
			bitmap.y = -TILE_HEIGHT;
			
			centerMap();
		}
		
		private function centerMap():void {
			var startX:int = SceneModel.inst.center.x - SCREEN_WIDTH/2 - TILE_WIDTH;
			var startY:int = SceneModel.inst.center.y - SCREEN_HEIGHT/2 - TILE_HEIGHT;
			var endX:int = SceneModel.inst.center.x + SCREEN_WIDTH/2 + TILE_WIDTH;
			var endY:int = SceneModel.inst.center.y + SCREEN_HEIGHT/2 + TILE_HEIGHT;
			
			loadTiles(startX,endX,startY,endY);
		}
		
		public function frameScript():void {
			if(getTimer()-lastTime>100) {
				var centerX:int = SceneModel.inst.center.x;
				var centerY:int = SceneModel.inst.center.y;
				lastTime = getTimer();
				//				this.buffer.scroll(0,-3);
				//				centerX -= 10;
				//				centerY += 3;
				if(lastScrollX == 0) lastScrollX = centerX;
				if(lastScrollY == 0) lastScrollY = centerY;
				
				if(Math.abs(lastScrollX - centerX)>TILE_WIDTH/2) {
					if(centerX - lastScrollX>0) {
						loadTiles(centerX + buffer.width/2 - (centerX - lastScrollX),centerX + buffer.width/2,centerY-buffer.height/2,centerY+buffer.height/2);
					}else {
						loadTiles(centerX - buffer.width/2,centerX - buffer.width/2 + lastScrollX - centerX,centerY-buffer.height/2,centerY+buffer.height/2);
					}
					lastScrollX = centerX;
				}
				if(Math.abs(lastScrollY - centerY)>TILE_HEIGHT/2) {
					if(centerY - lastScrollY>0) {
						loadTiles(centerX - buffer.width/2,centerX + buffer.width/2,centerY + buffer.height/2 -(centerY - lastScrollY),centerY + buffer.height/2);
					}else {
						loadTiles(centerX - buffer.width/2,centerX + buffer.width/2,centerY - buffer.height/2,centerY - buffer.height/2 + (lastScrollY - centerY));
					}
					lastScrollY = centerY;
				}
			}
		}
		
		private function loadTiles(startX:int,endX:int,startY:int,endY:int):void {
			var startCol:int = int(startX/TILE_WIDTH);
			startCol = betweenNumber(startCol,0,int(SCENE_WIDTH/TILE_WIDTH));
			var startRow:int = int(startY/TILE_HEIGHT);
			startRow = betweenNumber(startRow,0,int(SCENE_HEIGHT/TILE_HEIGHT));
			var endCol:int = endX%TILE_WIDTH == 0?int(endX/TILE_WIDTH)-1:int(endX/TILE_WIDTH);
			endCol = betweenNumber(endCol,0,int(SCENE_WIDTH/TILE_WIDTH));
			var endRow:int = endY%TILE_HEIGHT == 0?int(endY/TILE_HEIGHT)-1:int(endY/TILE_HEIGHT);
			endRow = betweenNumber(endRow,0,int(SCENE_HEIGHT/TILE_HEIGHT));
			
			for(var r:int=startRow;r<=endRow;r++) {
				for(var c:int=startCol;c<=endCol;c++) {
					var tile:TileBitmapData = new TileBitmapData(buffer);
					tile.col = c;
					tile.row = r;
					tile.thumbUrl = "assets/scene/"+SceneModel.inst.sceneVO.id+"/"+r+"_"+c+"_thumb.jpg";
					tile.url = "assets/scene/"+SceneModel.inst.sceneVO.id+"/"+r+"_"+c+".jpg";
				}
			}
		}
		
		private function betweenNumber(num:int,min:int,max:int):int {
			return Math.min(Math.max(min,num),max);
		}
	}
}